package com.guzhen.wallpager;

import android.content.Context;
import android.content.Intent;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.widget.Toast;

import com.fun.funlivewallpaper.MainActivity;
import com.fun.funlivewallpaper.MainActivity2;

import java.lang.ref.WeakReference;

public class Live1WallpaperService extends WallpaperService {
    public class MyEngine extends WallpaperService.Engine {
        private final Context ctx;
        private SurfaceView1 surfaceView;
        public  Live1WallpaperService service;
        public MyEngine(Context context0) {
            this.ctx = context0;
            if (context0 instanceof Live1WallpaperService) {
                service = (Live1WallpaperService) context0;
            }
        }
        private void setSufaceHolder(SurfaceHolder surfaceHolder) {
            if(this.surfaceView == null) {
                this.surfaceView = new SurfaceView1(this.ctx);
            }

            this.surfaceView.setSufaceHolder(surfaceHolder, new WallpaperEngineHolder(this));
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            this.setSufaceHolder(surfaceHolder);
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            this.surfaceView.surfaceCreated(holder);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.surfaceView.surfaceDestroyed(holder);
        }

        @Override
        public void onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            this.surfaceView.onTouchEvent(event);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            this.surfaceView.onVisibilityChanged(visible);
        }

        long lastStartTime = 0;

//        @Override
//        public void onTouchEvent(MotionEvent event) {
//            super.onTouchEvent(event);
//            Log.i("wallpaper", "onTouchEvent");
//
//            WeakReference<MainActivity> main = MainActivity.Companion.getMain();
//            if (main == null || main.get() == null || main.get().isDestroyed()) {
//                if (System.currentTimeMillis() - lastStartTime > 1000) {
//                    lastStartTime = System.currentTimeMillis();
//                    try {
//                        Intent intent = new Intent(ctx, MainActivity2.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        ctx.startActivity(intent);
//                    } catch (Exception e) {
//                        Toast.makeText(ctx, "not support", Toast.LENGTH_SHORT).show();
//                    }
//                }
//            }else{
//                Toast.makeText(ctx, "主应用未销毁", Toast.LENGTH_SHORT).show();
//            }
//
//        }
    }
    @Override
    public Engine onCreateEngine() {
        return new MyEngine(this);
    }
}